package com.ruoyi.evalution.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.evalution.domain.LabEvalutionTarget;
import com.ruoyi.evalution.domain.LabEvalutionTopic;
import com.ruoyi.evalution.domain.LabEvalutionTopicTask;
import com.ruoyi.evalution.mapper.LabEvalutionTopicMapper;
import com.ruoyi.evalution.mapper.LabEvalutionTopicTaskMapper;
import com.ruoyi.evalution.service.ILabEvalutionTopicService;
import com.ruoyi.labdata.domain.LabFile;
import com.ruoyi.labdata.service.ILabFileService;
import com.ruoyi.system.utils.FileTypeEnum;

/**
 * 实验室评测Service业务层处理
 * 
 * @author winner pan
 * @date 2022-08-03
 */
@Service
public class LabEvalutionTopicServiceImpl implements ILabEvalutionTopicService {
	private static final String belongType = FileTypeEnum.EvalutionTopic.getValue();
	@Autowired
	private LabEvalutionTopicMapper labEvalutionTopicMapper;
	@Autowired
	private LabEvalutionTopicTaskMapper labEvalutionTopicTaskMapper;
	@Autowired
	private ILabFileService labFileService;
	/**
	 * 查询实验室评测
	 * 
	 * @param instanceId
	 *            实验室评测ID
	 * @return 实验室评测
	 */
	@Override
	public LabEvalutionTopic selectLabEvalutionTopicById(Long instanceId) {
		List<LabFile> labFiles = labFileService.getFiles(instanceId, belongType);
		LabEvalutionTopic topic = labEvalutionTopicMapper.selectLabEvalutionTopicById(instanceId);
		topic.setImages(labFiles);
		LabEvalutionTopicTask query = new LabEvalutionTopicTask();
		query.setInstanceId(instanceId);
		query.setScoreType(1);
		topic.setTopicTaskList(labEvalutionTopicTaskMapper.selectLabEvalutionTopicTaskList(query));
		return topic;
	}

	/**
	 * 查询实验室评测列表
	 * 
	 * @param labEvalutionTopic
	 *            实验室评测
	 * @return 实验室评测
	 */
	@Override
	public List<LabEvalutionTopic> selectLabEvalutionTopicList(LabEvalutionTopic labEvalutionTopic) {
		return labEvalutionTopicMapper.selectLabEvalutionTopicList(labEvalutionTopic);
	}

	/**
	 * 新增实验室评测
	 * 
	 * @param labEvalutionTopic
	 *            实验室评测
	 * @return 结果
	 */
	@Override
	@Transactional
	public int insertLabEvalutionTopic(LabEvalutionTopic labEvalutionTopic) {
		labEvalutionTopic.setCreateTime(DateUtils.getNowDate());
		int ret = labEvalutionTopicMapper.insertLabEvalutionTopic(labEvalutionTopic);
		labFileService.insertFiles(labEvalutionTopic.getImages(), labEvalutionTopic.getInstanceId(), belongType);
		labEvalutionTopic.getTopicTaskList().forEach(item->{
			LabEvalutionTopicTask labEvalutionTopicTask = new LabEvalutionTopicTask();
			labEvalutionTopicTask.setInstanceId(labEvalutionTopic.getInstanceId());
			labEvalutionTopicTask.setScoreType(Integer.valueOf(1));
			labEvalutionTopicTask.setNickName(item.getNickName());
			labEvalutionTopicTask.setUserName(item.getUserName());
			labEvalutionTopicTask.setOrgId(item.getOrgId());
			labEvalutionTopicTask.setOrgName(item.getOrgName());
			labEvalutionTopicTask.setStatus(0);
			this.labEvalutionTopicTaskMapper.insertLabEvalutionTopicTask(labEvalutionTopicTask);
		});
		return ret;
	}

	/**
	 * 修改实验室评测
	 * 
	 * @param labEvalutionTopic
	 *            实验室评测
	 * @return 结果
	 */
	@Override
	@Transactional
	public int updateLabEvalutionTopic(LabEvalutionTopic labEvalutionTopic) {
		labFileService.updateFiles(labEvalutionTopic.getImages(), labEvalutionTopic.getInstanceId(), belongType);
		if(labEvalutionTopic.getStatus()==1){
			labEvalutionTopic.setFinishedTime(DateUtils.getNowDate());
		}
		this.labEvalutionTopicTaskMapper.deleteLabEvalutionTopicTaskByInstanceId(labEvalutionTopic.getInstanceId());
		labEvalutionTopic.getTopicTaskList().forEach(item->{
			LabEvalutionTopicTask labEvalutionTopicTask = new LabEvalutionTopicTask();
			labEvalutionTopicTask.setInstanceId(labEvalutionTopic.getInstanceId());
			labEvalutionTopicTask.setScoreType(Integer.valueOf(1));
			labEvalutionTopicTask.setNickName(item.getNickName());
			labEvalutionTopicTask.setUserName(item.getUserName());
			labEvalutionTopicTask.setOrgId(item.getOrgId());
			labEvalutionTopicTask.setOrgName(item.getOrgName());
			labEvalutionTopicTask.setStatus(0);
			this.labEvalutionTopicTaskMapper.insertLabEvalutionTopicTask(labEvalutionTopicTask);
		});
		return labEvalutionTopicMapper.updateLabEvalutionTopic(labEvalutionTopic);
	}

	/**
	 * 批量删除实验室评测
	 * 
	 * @param instanceIds
	 *            需要删除的实验室评测ID
	 * @return 结果
	 */
	/*@Override
	public int deleteLabEvalutionTopicByIds(Long[] instanceIds) {
		return labEvalutionTopicMapper.deleteLabEvalutionTopicByIds(instanceIds);
	}*/

	/**
	 * 删除实验室评测信息
	 * 
	 * @param instanceId
	 *            实验室评测ID
	 * @return 结果
	 */
	@Override
	@Transactional
	public int deleteLabEvalutionTopicById(Long instanceId) {
		labFileService.deleteLabFile(instanceId, belongType);
		this.labEvalutionTopicTaskMapper.deleteLabEvalutionTopicTaskByInstanceId(instanceId);
		return labEvalutionTopicMapper.deleteLabEvalutionTopicById(instanceId);
	}
}
